﻿///<reference path="../jquery/jquery.js"/>
/*-------------------------------------------------------------------------------
												VoteList.js for Pages/Votes.aspx
--------------------------------------------------------------------------------*/
var VoteList = function() {
	//private area
	var _ajaxDefaultOpts = {
		type: 'post',
		dataType: 'json',
		contentType: 'application/json;charset=utf-8',
		cache: false
	};
	var _defaultDialogOpts = {
		modal: true,
		width: 600,
		height: 400,
		resizable: false,
		overlay: { opacity: 0.5, background: 'black' }
	};
	//public area
	return {
		InitPager: "",
		Asmx: "",
		ViewPath: "~/Views/VoteListView.ascx",
		HasItems: true,
		CurrentVotingID: null,
		Init: function(opts) {
			VoteList.Asmx = opts.Asmx || "";
			VoteList.InitPager = opts.InitPagerStr || "";
			VoteList.ViewPath = opts.ViewPath || TopicList.ViewPath;
			//Do first load.
			VoteList.Select($.evalJSON(VoteList.InitPager));
		}, // /Init
		Select: function(opts) {
			var data = {};
			data.CPage = opts.CurrentPage;
			data.RPPage = opts.RowPerPage;
			data.ViewPath = VoteList.ViewPath;
			var ajaxOpts = _ajaxDefaultOpts;
			ajaxOpts.url = VoteList.Asmx + "SelectVotes";
			ajaxOpts.success = function(msg) {
				msg = msg.d;
				if (msg.IsOk) {
					//Replace TopicList
					$("#votesWrapper").html(msg.Context.VoteListHtml);
					//Replace pager
					$("#pagerWrapper").html(msg.Context.PagerHtml);
					//Init pager again
					if (msg.Context.Pager) {
						SimplePager.Init(msg.Context.Pager, VoteList.Select);
					};
					//scroll to the latest record
					if (msg.Context.TopItemID) {
						var topItemDivID = "vote_" + msg.Context.TopItemID; //topItemID was determined by VoteView.ascx.
						$.scrollTo("#" + topItemDivID);
					};
					VoteList.HasItems = msg.Context.HasItems;
				} else {
					alert("Something goes wrong!");
				}
			};
			ajaxOpts.data = $.toJSON({ Params: data });
			$.ajax(ajaxOpts);
			ajaxOpts = null;
		}, //endof Select
		ShowVoteItems: function(opts) {
			var data = { id: opts.id };
			VoteList.CurrentVotingID = opts.id;
			var dialogOpts = _defaultDialogOpts;
			dialogOpts.close = function() {
				$("#toolsWrapper").append($("#popupBox").dialog("destroy").empty().remove());
			};
			var ajaxopts = _ajaxDefaultOpts;
			ajaxopts.url = VoteList.Asmx + "ShowVoteItems";
			ajaxopts.data = $.toJSON({ Params: data });
			ajaxopts.success = function(msg) {
				msg = msg.d;
				if (msg.IsOk) {
					$("#popupBox").html(msg.Body).show().dialog(dialogOpts);
				} else {
					alert(msg.Body);
				};
			};
			$.ajax(ajaxopts);
			ajaxopts = null;
		}, // /ShowVoteItems
		DoVote: function(opts) {
			if ($(".rateItem", "#rateList").length == 0) {
				alert("没有任何投票项");
				return;
			};
			var data = $("input:checked", "#rateList").serializeArray();
			if (data.length == 0) {
				alert("请选择投票项");
				return;
			};
			data.push({ name: 'VoteID', value: opts.id });
			var ajaxopts = _ajaxDefaultOpts;
			ajaxopts.url = VoteList.Asmx + "DoVote";
			ajaxopts.data = $.toJSON({ Params: data });
			ajaxopts.success = function(msg) {
				$("#btnDoVote").attr("value", "投 票").attr("disabled", false);
				msg = msg.d;
				if (msg.IsOk) {
					//close the dialog
					$("#popupBox").dialog("close");
					//update Raters' number
					$("span.num", "#vote_" + VoteList.CurrentVotingID).html(msg.Context.Hits);
				} else {
					alert(msg.Body);
				};
			};

			$.ajax(ajaxopts);
			ajaxopts = null;
			$("#btnDoVote").attr("value", "投票ing..").attr("disabled", true);
		} // /DoVote
	};
} ();